package com.chengxiaoxiao.dao.impl;

import com.chengxiaoxiao.dao.ConfigurationDao;
import com.chengxiaoxiao.domain.Configuration;
import com.chengxiaoxiao.utils.db.BeanHandler;
import com.chengxiaoxiao.utils.db.BeanListHandler;
import com.chengxiaoxiao.utils.db.DBAssist;
import com.chengxiaoxiao.utils.db.DBCPUtil;

import java.util.List;

public class ConfigurationDaoImpl implements ConfigurationDao {
    private DBAssist da = new DBAssist(DBCPUtil.getDataSource());

    @Override
    public void add(Configuration configuration) {
        String sql = "insert into configuration(`key`,value) values(?,?)";
        da.update(sql, configuration.getKey(), configuration.getValue());
    }

    @Override
    public void update(Configuration configuration) {
        String sql = "update configuration set `key`=?,value=? where id=?";
        da.update(sql, configuration.getKey(), configuration.getValue(), configuration.getId());
    }

    @Override
    public void delete(long id) {
        String sql = "delete from configuration where id=?";
        da.update(sql, id);
    }

    @Override
    public List get() {
        return (List) da.query("select * from configuration", new BeanListHandler(Configuration.class));

    }

    @Override
    public Configuration getbyId(long id) {
        return (Configuration) da.query("select * from configuration where id=?", new BeanHandler(Configuration.class), id);
    }

    @Override
    public List getListPage(int startIndex, int pageSize) {
        return null;
    }

    @Override
    public Configuration getValueByKey(String key) {
        Configuration config = (Configuration) da.query("select * from configuration where `key`=?", new BeanHandler(Configuration.class), key);

        return config;
    }

    @Override
    public String getValue(String key) {

        Configuration config = (Configuration) da.query("select * from configuration where `key`=?", new BeanHandler(Configuration.class), key);

        return config == null ? "" : config.getValue();
    }
}
